/**
 * Created by Yamazhiki on 08/01/2017.
 */

export enum CType{
    Char   = 1,
    UChar  = 1,
    Short  = 2,
    UShort = 2,
    Int    = 4,
    UInt   = 4,
    Int64  = 8,
    UInt64 = 8
}

export class SocketHelper {
    private offset: number = 0

    /**
     *
     * @param offest 初始偏移量
     */
    constructor(offest: number = 0) {
        this.offset = offest
    }

    putChar(buffer: Buffer, value: number) {
        buffer.writeInt8(value, this.offset)
        this.offset++
    }

    putUChar(buffer: Buffer, value: number) {
        buffer.writeUInt8(value, this.offset)
        this.offset++
    }

    putShort(buffer: Buffer, value: number) {
        buffer.writeUInt16LE(value, this.offset)
        this.offset += 2
    }

    putUShort(buffer: Buffer, value: number) {
        buffer.writeUInt16LE(value, this.offset)
        this.offset += 2
    }

    putInt(buffer: Buffer, value: number) {
        buffer.writeInt32LE(value, this.offset)
        this.offset += 4
    }

    putUInt(buffer: Buffer, value: number) {
        buffer.writeUInt32LE(value, this.offset)
        this.offset += 4
    }

    putInt64(buffer: Buffer, value: number) {
        buffer.writeInt32LE(value, this.offset)
        this.offset += 8
    }

    putUInt64(buffer: Buffer, value: number) {
        buffer.writeInt32LE(value, this.offset)
        this.offset += 8
    }

    putString(buffer: Buffer, value: string, len: number) {
        if (value) {
            buffer.write(value, this.offset, len)
        } else {
            buffer.fill(0, this.offset, this.offset + len)
        }
        this.offset += len
    }

    getChar(buffer: Buffer): number {
        let result = buffer.readInt8(this.offset)
        this.offset++
        return result
    }

    getUChar(buffer: Buffer): number {
        let result = buffer.readUInt8(this.offset)
        this.offset++
        return result
    }

    getShort(buffer): number {
        let result = buffer.readInt16LE(this.offset)
        this.offset += 2
        return result
    }

    getUShort(buffer: Buffer): number {
        let result = buffer.readUInt16LE(this.offset)
        this.offset += 2
        return result
    }

    getInt(buffer: Buffer): number {
        let result = buffer.readInt32LE(this.offset)
        this.offset += 4
        return result
    }

    getUInt(buffer: Buffer): number {
        let result = buffer.readUInt32LE(this.offset)
        this.offset += 4
        return result
    }

    getInt64(buffer: Buffer): number {
        let result = buffer.readUInt32LE(this.offset)
        this.offset += 8
        return result
    }

    getUInt64(buffer: Buffer): number {
        let result = buffer.readUInt32LE(this.offset)
        this.offset += 8
        return result
    }

    getString(buffer: Buffer, len: number, encoding = "ascii"): string {
        let strBuffer = buffer.slice(this.offset, this.offset + len)
        let useLen = 0
        for (let i = 0; i < strBuffer.byteLength; i++) {
            if (strBuffer[i] === 0) break
            useLen++
        }
        this.offset += len
        return strBuffer.toString(encoding, 0, useLen)
    }

    getCharArray(buffer: Buffer, len: number): number[] {
        let tmpBuffer = buffer.slice(this.offset, this.offset + len)
        let rlt = new Array<number>()
        for (let i = 0; i < tmpBuffer.byteLength; i++) {
            rlt.concat(tmpBuffer.readInt8(i))
        }
        this.offset += len
        return rlt
    }

    getShortArray(buffer: Buffer, len: number): number[] {
        let finalOffset = len * 2
        let tempBuffer = buffer.slice(this.offset, this.offset + finalOffset)
        let rlt = new Array<number>()
        for (let i = 0; i < tempBuffer.byteLength; i += 2) {
            rlt.splice(i / 2, 0, tempBuffer.readInt16LE(i))
        }
        this.offset += finalOffset
        return rlt
    }


    getBuffer(buffer: Buffer, len: number): Buffer {
        let rlt = buffer.slice(len)
        this.offset += len
        return rlt
    }

}